package com.ateam.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ateam.biz.ProjectsBiz;
import com.ateam.biz.UsersBiz;
import com.ateam.pojo.Companies;
import com.ateam.pojo.Projects;
import com.ateam.pojo.Users;
import com.ateam.util.SplitFactory;

/**
 * @author WuYuan
 * 
 */
@Controller
public class ProjectsController {
	@Resource(name = "projectsBiz")
	private ProjectsBiz projectsBiz;

	@Resource(name = "usersBiz")
	private UsersBiz usersBiz;

	// 公司内部人员创建新项目
	@ResponseBody
	@RequestMapping("createproject.do")
	public boolean createproject(Projects project, HttpSession session, HttpServletRequest request) {
		System.out.println(project.toString());
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users user = (Users) session.getAttribute("currentUser");
				if (0 != projectsBiz.addProject(project, user.getUserid())) {
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 查看用户当前加入的项目
	@ResponseBody
	@RequestMapping("shownprojects.do")
	public List<Projects> showProjectsByUser(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users user = (Users) session.getAttribute("currentUser");
				return projectsBiz.getProjectsByUser(user.getUserid());
			} else
				return null;
		} else
			return null;
	}

	// 查看项目具体内容
	@RequestMapping("gotoprojectdetail.do")
	public String projectdetail(String projectid, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Projects currentProject = projectsBiz.getProjectDetail(Integer.parseInt(projectid));
				request.setAttribute("currentProject", currentProject);
				return "projectdetail";
			} else
				return "index";
		} else
			return "index";
	}

	// 完成一个项目
	@ResponseBody
	@RequestMapping("completeproject.do")
	public boolean completeproject(String projectid, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				if (0 != projectsBiz.updateProjectComplete(Integer.parseInt(projectid))) {
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 获取项目中的所有人
	@ResponseBody
	@RequestMapping("projectgetallusers.do")
	public List<Users> getProjectAllUsers(String projectid, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				List<Users> users = usersBiz.getUsersByProjectid(Integer.parseInt(projectid));
				return users;
			} else
				return null;
		} else
			return null;
	}

	// 获得还没有加入项目的人
	@ResponseBody
	@RequestMapping("intviteworkernotinproject.do")
	public List<Users> intviteworkernotinproject(String projectid, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Companies currentCompany = (Companies) session.getAttribute("currentCompany");
				return usersBiz.getUsersByNoProjectid(currentCompany.getCompanyid(), Integer.parseInt(projectid));
			} else
				return null;
		} else
			return null;
	}

	// 邀请公司的人来项目
	@ResponseBody
	@RequestMapping("inviteprojectworker.do")
	public boolean inviteprojectworker(String projectid, String projectinvitedmemberid, HttpSession session,
			HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				int[] memberids = SplitFactory.getIntarray(projectinvitedmemberid);// 切割参与者id
				if (0 != usersBiz.addUserProject(Integer.parseInt(projectid), memberids)) {
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}
}
